package com.ruoyi.quanzi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunMessage;
import com.ruoyi.quanzi.service.ISunMessageService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 消息Controller
 * 
 * @author ruoyi
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/quanzi/message")
public class SunMessageController extends BaseController
{
    @Autowired
    private ISunMessageService sunMessageService;

    /**
     * 查询消息列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:list')")
    @GetMapping("/list")
    public TableDataInfo list(SunMessage sunMessage)
    {
        startPage();
        List<SunMessage> list = sunMessageService.selectSunMessageList(sunMessage);
        return getDataTable(list);
    }

    /**
     * 导出消息列表
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:export')")
    @Log(title = "消息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SunMessage sunMessage)
    {
        List<SunMessage> list = sunMessageService.selectSunMessageList(sunMessage);
        ExcelUtil<SunMessage> util = new ExcelUtil<SunMessage>(SunMessage.class);
        util.exportExcel(response, list, "消息数据");
    }

    /**
     * 获取消息详细信息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunMessageService.selectSunMessageById(id));
    }

    /**
     * 新增消息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:add')")
    @Log(title = "消息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SunMessage sunMessage)
    {
        return toAjax(sunMessageService.insertSunMessage(sunMessage));
    }

    /**
     * 修改消息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:edit')")
    @Log(title = "消息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SunMessage sunMessage)
    {
        return toAjax(sunMessageService.updateSunMessage(sunMessage));
    }

    /**
     * 删除消息
     */
    @PreAuthorize("@ss.hasPermi('quanzi:message:remove')")
    @Log(title = "消息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunMessageService.deleteSunMessageByIds(ids));
    }

    /**
     * 获取用户消息列表
     */
    @GetMapping("/user/{userId}")
    public AjaxResult getUserMessages(@PathVariable("userId") Long userId)
    {
        List<SunMessage> list = sunMessageService.selectSunMessageByUserId(userId);
        return success(list);
    }

    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count/{userId}")
    public AjaxResult getUnreadCount(@PathVariable("userId") Long userId)
    {
        int count = sunMessageService.selectUnreadMessageCount(userId);
        return success(count);
    }

    /**
     * 根据类型获取消息
     */
    @GetMapping("/type/{userId}/{type}")
    public AjaxResult getMessagesByType(@PathVariable("userId") Long userId, @PathVariable("type") Integer type)
    {
        List<SunMessage> list = sunMessageService.selectMessagesByType(userId, type);
        return success(list);
    }

    /**
     * 标记消息为已读
     */
    @PutMapping("/read/{id}")
    public AjaxResult markAsRead(@PathVariable("id") Long id)
    {
        return toAjax(sunMessageService.markAsRead(id));
    }

    /**
     * 标记所有消息为已读
     */
    @PutMapping("/read/all/{userId}")
    public AjaxResult markAllAsRead(@PathVariable("userId") Long userId)
    {
        return toAjax(sunMessageService.markAllAsRead(userId));
    }
}